package com.example.crm_system.service.impl;

import com.example.crm_system.entity.Customer;
import com.example.crm_system.entity.Params;
import com.example.crm_system.entity.WorkOrder;
import com.example.crm_system.exception.MyOwnException;
import com.example.crm_system.mapper.WorkOrderMapper;
import com.example.crm_system.service.BusinessService;
import com.example.crm_system.service.CustomerService;
import com.example.crm_system.service.WorkOrderService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class WorkOrderServiceImpl implements WorkOrderService {

    @Resource
    private WorkOrderMapper workOrderMapper;
    @Resource
    private CustomerService customerService;
    @Resource
    private BusinessService businessService;

    @Override
    public void insert(WorkOrder workOrder) {

        if (workOrder.getCno() == null || customerService.selectByCno(workOrder.getCno()) == null) {
            throw new MyOwnException("客户编号不能为空");
        }
        if (workOrder.getBno() == null || businessService.selectByBno(workOrder.getBno()) == null) {
            throw new MyOwnException("业务编号不能为空");
        }
        Params params = new Params();
        params.setCno(workOrder.getCno());
        params.setBno(workOrder.getBno());
        params.setStatus("CREATED");
        List<WorkOrder> status = workOrderMapper.findByWorkOrder(params);
        if (!status.isEmpty()) {
            throw new MyOwnException("该工单已存在");
        }
        workOrderMapper.insert(workOrder);
    }

    @Override
    public void update(WorkOrder workOrder) {
        if (workOrder.getCno() == null || customerService.selectByCno(workOrder.getCno()) == null) {
            throw new MyOwnException("客户编号不能为空");
        }
        if (workOrder.getBno() == null || businessService.selectByBno(workOrder.getBno()) == null) {
            throw new MyOwnException("业务编号不能为空");
        }
        Params params = new Params();
        params.setCno(workOrder.getCno());
        params.setBno(workOrder.getBno());
        params.setStatus("CREATED");
        List<WorkOrder> status = workOrderMapper.findByWorkOrder(params);
        if (!status.isEmpty()) {
            throw new MyOwnException("该工单已存在");
        }
        workOrderMapper.update(workOrder);
    }

    @Override
    public void complete(Integer ono) { workOrderMapper.complete(ono); }

    @Override
    public void cancel(Integer ono) { workOrderMapper.cancel(ono); }

    @Override
    public void delete(Integer ono) {
        workOrderMapper.delete(ono);
    }

    @Override
    public PageInfo<WorkOrder> searchWorkOrder(Params params) {
        // 开启分页查询功能
        PageHelper.startPage(params.getPageNum(), params.getPageSize());
        // 接下来的查询会自动按照当前开启的分页设置来查询
        List<WorkOrder> list = workOrderMapper.searchWorkOrder(params);
        return PageInfo.of(list);
    }
}
